package org.zfes.snowier.cloud.zbss.cms.content.service.impl;

import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.content.service.ICmsContentUserDefineAttrService;
import org.zfes.snowier.cloud.zbss.cms.content.dao.CmsContentUserDefineAttrMapper;
import org.zfes.snowier.cloud.zbss.cms.content.model.CmsContentUserDefineAttr;
import java.util.stream.Stream;
@Service
public class CmsContentUserDefineAttrServiceImpl implements ICmsContentUserDefineAttrService{
 	@Autowired
	private CmsContentUserDefineAttrMapper cmsContentUserDefineAttrMapper;
	
 	
 	
 	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createOrUpdateUserDefineAttr(Long contentId,Map<String ,String> userDefineAttrMap) {
 		ZAssert.notNull(contentId, "内容id不能为空");
 		if(!ZObjectUtil.isEmpty(userDefineAttrMap)) {
 			
 			userDefineAttrMap.forEach((key,val)->{
 				
 				CmsContentUserDefineAttr attrInDB=cmsContentUserDefineAttrMapper.selectByContentIdAndKey(contentId, key);
 				if(ZObjectUtil.isNull(attrInDB)) {
 					
 					CmsContentUserDefineAttr userDefineAttr=new CmsContentUserDefineAttr( contentId,  key,  val);
 	 				createCmsContentUserDefineAttr(userDefineAttr) ;
 				}else {
 					
 					attrInDB.setAttrValue(val);
 					updateCmsContentUserDefineAttr( attrInDB);
 				}
 				
 			});
 		}
 	
	}
 	
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsContentUserDefineAttr(CmsContentUserDefineAttr cmsContentUserDefineAttr) {
		ZBeanUtil.setCreateInfo(cmsContentUserDefineAttr);
		ZBeanUtil.validateBean(cmsContentUserDefineAttr);
		cmsContentUserDefineAttrMapper.insertSelective(cmsContentUserDefineAttr);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsContentUserDefineAttr> loadCmsContentUserDefineAttrById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsContentUserDefineAttrMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsContentUserDefineAttr(CmsContentUserDefineAttr cmsContentUserDefineAttr) {
		ZBeanUtil.validateBean(cmsContentUserDefineAttr);
		cmsContentUserDefineAttr.setUpdateTime(ZDateUtil.newDate());
		cmsContentUserDefineAttrMapper.updateByPrimaryKeySelective(cmsContentUserDefineAttr);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsContentUserDefineAttr(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsContentUserDefineAttrMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentUserDefineAttrStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentUserDefineAttrMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsContentUserDefineAttrDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsContentUserDefineAttrMapper.selectListCount(pm), cmsContentUserDefineAttrMapper.selectMapListPage(pm));
		return ds;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsContentUserDefineAttrStatusByContentId(Long contentId, Byte status) {
		ZAssert.notNull(contentId, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsContentUserDefineAttrMapper.updateStatusByContentId(contentId, status);
	}
	
}